package com.relax.mybatis.encrypt.algorithm;

import com.relax.mybatis.constant.EncryptConstants;

/**
 * @author relax
 * @date 2022/07/05
 * @desc Base64加密
 */
public class Base64 {

    public static byte[] encode(byte[] data) {
        if (data.length == 0) {
            return data;
        }
        return java.util.Base64.getEncoder().encode(data);
    }

    public static byte[] encode(String data) {
        return encode(data.getBytes(EncryptConstants.DEFAULT_CHARSET));
    }

    public static String encodeToString(byte[] data) {
        if (data.length == 0) {
            return "";
        }
        return new String(encode(data), EncryptConstants.DEFAULT_CHARSET);
    }

    public static String encodeToString(String data) {
        if (data.isEmpty()) {
            return "";
        }
        return encodeToString(data.getBytes(EncryptConstants.DEFAULT_CHARSET));
    }

    public static byte[] decode(byte[] data) {
        if (data.length == 0) {
            return data;
        }
        return java.util.Base64.getDecoder().decode(data);
    }

    public static byte[] decode(String data) {
        return decode(data.getBytes(EncryptConstants.DEFAULT_CHARSET));
    }

    public static String decodeToString(byte[] data) {
        if (data.length == 0) {
            return "";
        }
        return new String(decode(data), EncryptConstants.DEFAULT_CHARSET);
    }

    public static String decodeToString(String data) {
        if (data.isEmpty()) {
            return "";
        }
        return decodeToString(data.getBytes(EncryptConstants.DEFAULT_CHARSET));
    }

}
